package com.yan.student.service;

import com.yan.student.dao.StuDao;
import com.yan.student.domain.Student;

import java.util.Scanner;

public class StudentServicelmpl implements StudentService{
    public static final Scanner INPUT = new Scanner(System.in);
    @Override
    public void addStudent(Student student) {
        StuDao.studentsList.add(student);
        System.out.println("添加成功");
    }

    @Override
    public void findStudent() {
        if(StuDao.studentsList.size() == 0){
            System.out.println("无信息，请先添加信息在查询");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t居住地址\t");
        for (Student student : StuDao.studentsList) {
            System.out.println(student.getSid() + "\t\t" + student.getName() + "\t\t"+ student.getAge() + "岁\t\t"+ student.getaddress());
        }
    }

    @Override
    public void upDate(String id) {
        int index = getIndex(id);
        if(index != - 1){
            System.out.println("请输入学生新姓名");
            String newName = INPUT.next();
            StuDao.studentsList.get(index).setName(newName);
            System.out.println("请输入学生新年龄");
            int newAge = INPUT.nextInt();
            StuDao.studentsList.get(index).setAge(newAge);
            System.out.println("请输入学生新居住地址");
            String newAddress = INPUT.next();
            StuDao.studentsList.get(index).setaddress(newAddress);
            System.out.println("修改学生成功");
        }else {
            System.out.println("信息不存在");
        }
    }

    @Override
    public void deletDate(String id) {
        int index = getIndex(id);
        if(index != -1){
            StuDao.studentsList.remove(StuDao.studentsList.get(index));
            System.out.println("删除成功");
        } else {
            System.out.println("该信息不存在,请重新输入");
        }
    }


    @Override
    public int getIndex(String target) {
        for (int i = 0; i < StuDao.studentsList.size(); i++) {
            if(StuDao.studentsList.get(i).getSid().equals(target)){
                return i;
            }
        }
        return -1;
    }
}
